package com.yyfax.ph.home.banner;

import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * 获取广告
 */
public class BannerPresenter extends Presenter<BannerModel, BannerPresenter.View> {

	public static final String REQUEST_BANNER_TYPE_NEW = "3"; // 新版横屏Banner图.

	private View mView;

	@Override
	public void attach(View view) {
		mView = view;
		mModel = new BannerModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public interface View extends IView {
		void onGetBannerSuccess(List<BannerModel.BannerBean> bannerBeanList);

		void onGetBannerFailure(String tips);
	}

	public static class Generator implements PresenterFactory<BannerPresenter> {
		@Override
		public BannerPresenter create() {
			return new BannerPresenter();
		}
	}

	/**
	 * 获取广告图片
	 */
	public void getBanner() {
		BannerModel.BannerParams bannerParams = new BannerModel.BannerParams();
		bannerParams.type = REQUEST_BANNER_TYPE_NEW;
		execute(bannerParams,FUNCTIONAL_CODE, false);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void getBannerResult(BannerModel.AdResult result) {
		if (isResponseSuccess(result)) {
			List<BannerModel.BannerBean> bannerBeanList = result.getContent();
			mView.onGetBannerSuccess(bannerBeanList);
		} else {
			mView.onGetBannerFailure(result.getMsg());
		}
	}
}
